package com.example.easybuy.controller;

import com.example.easybuy.pojo.User;
import com.example.easybuy.service.UserService;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    private Logger logger=Logger.getLogger(getClass());

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String fileName)throws IOException {
        if(null!=fileName) {
            File file =new File(fileName);
            HttpHeaders headers=new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attrachment", fileName);
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
        }
        return null;

    }

    /*注册用户*/
    @RequestMapping("addUser")
    @ResponseBody
    @CrossOrigin
    public String addUser(User user){
        logger.debug("LoginController-addUser-start...user:"+user);
        int addUserRow=userService.addUser(user);
        String success=null;
        if(addUserRow>0){
            success="添加成功！";
        }else{
            success ="添加失败！";
        }
        logger.debug("LoginController-addUser-success:"+success);
        return success;
    }

    /*查询登录名是否存在*/
    @RequestMapping("getLoginName")
    @ResponseBody
    @CrossOrigin
    public String getLoginName(String loginName){
        logger.debug("LoginController-getLoginName-start...loginName:"+loginName);
        String isExist=userService.getLoginName(loginName);
        logger.debug("LoginController-userService.getLoginName-...isExist:"+isExist);
        return isExist;
    }

    /*获取注册验证码*/
    @RequestMapping("getIpt")
    @ResponseBody
    @CrossOrigin
    public String getIpt(){
        logger.debug("LoginController-getIpt-start...");
        int max=10000;
        int min=1000;
        Random random=new Random();
        int Ipt=random.nextInt(max-min)+min+1;
        logger.debug("LoginController-getIpt-Ipt验证码："+Ipt);
        String iptNum=String.valueOf(Ipt);
        return iptNum;
    }

    /**
     * 查询用户是否存在？
     * @param
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    @CrossOrigin
    public String login(String loginName, String password, HttpServletResponse response){
        logger.debug("LoginController login loginName: "+loginName+" password: "+password);
        String isUser =userService.login(loginName,password,response);
        logger.debug("LoginController-login()-isUser:"+isUser);
        return isUser;
    }


    /**
     * 在redis里拿出当前登录的User对象
     */
    @RequestMapping("getUser")
    @ResponseBody
    @CrossOrigin
    public User getUser(){
        logger.debug("LoginController obtainUser ");
        User user =userService.getUser();
        logger.debug("LoginController obtainUser-user："+user);
        return user;
    }


}
